<template>
  <div class="edit-container">
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.go(-1)" />
    <van-image round fit="cover" :src="userInfo.photo" />
    <!--文件上传 -->
    <van-uploader :after-read="afterRead" />
    <van-cell-group>
      <van-cell
        title="名称"
        is-link
        :value="userInfo.name"
        @click="showDialogclick"
      />
      <van-cell
        title="性别"
        is-link
        :value="userInfo.gender ? '女' : '男'"
        @click="showPopup = true"
      />
      <van-cell
        title="生日"
        is-link
        :value="userInfo.birthday"
        @click="showDatePick = true"
      />
    </van-cell-group>
    <!-- 修改昵称的对话框 -->
    <van-dialog
      v-model="showDialog"
      title="修改昵称"
      show-cancel-button
      @confirm="updateNickname"
    >
      <!-- 弹框里面输入框 -->
      <van-field
        ref="namefiled"
        placeholder="请输入昵称"
        v-model="newName"
      ></van-field>
    </van-dialog>
    <!-- 修改性别弹窗 -->
    <van-popup v-model="showPopup" position="bottom" :style="{ height: '30%' }">
      <van-nav-bar
        title="修改性别"
        left-arrow
        @click-left="showPopup = false"
      />
      <van-cell title="男" is-link @click="updateGender(0)" />
      <van-cell title="女" is-link @click="updateGender(1)" />
    </van-popup>
    <!-- 修改生日 -->
    <van-popup position="bottom" v-model="showDatePick">
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        title="选择年月日"
        :min-date="minDate"
        :max-date="maxDate"
        @cancel="showDatePick = false"
        @confirm="updateBirthday"
      />
    </van-popup>
    <!-- 裁切盒子 -->
    <div class="cropper-box" v-show="showCropper">
      <vueCropper
        ref="cropper"
        fixed
        :img="img"
        autoCropWidth="120"
        autoCropHeight="120"
        autoCrop
      ></vueCropper>
      <van-button @click="submitPhoto" class="confirm-btn" type="primary"
        >确认</van-button
      >
      <van-button @click="showCropper = false" class="cancel-btn" type="info"
        >取消</van-button
      >
    </div>
  </div>
</template>

<script>
//导入mapState辅助函数
import { mapState } from "vuex";
import { editUserInfo, editUserAvatar } from "../user/user";
import { VueCropper } from "vue-cropper";
import moment from "moment";
export default {
  name: "edit",
  data() {
    return {
      //隐藏编辑框
      showDialog: false,
      //输入文本框
      newName: "",
      //性别隐藏框
      showPopup: false,
      //生日隐藏框
      showDatePick: false,
      //日期最小值，vant默认格式
      minDate: new Date(2020, 0, 1),
      //日期最大值，vant默认格式
      maxDate: new Date(2025, 10, 1),
      //当前选中日期
      currentDate: new Date(),
      //剪切的图片
      img: "",
      showCropper: false,
    };
  },
  computed: {
    //隐射成计算属性
    ...mapState(["userInfo"]),
  },
  datePickerClick(){
    this.showDatePicker = true
    let data = new Date(this.userInfo.birthday)
    this.currentDate = date
  },
  methods: {
    //修改名字
    updateNickname() {
      editUserInfo({ name: this.newName }).then((res) => {
        //轻提示弹窗
        this.$toast.success("更新成功！");
        //更新vuex数据
        this.$store.commit("setUserInfo", {
          ...this.userInfo, //先展开数据,查看数据是否纰漏
          name: this.newName, //新的name覆盖原始name
        });
      });
    },
    showDialogclick() {
      //显示编辑框
      this.showDialog = true;
      //设置输入文本框
      this.newName = this.userInfo.name;
      //实现文本框聚焦功能使用原生的focus会报错，页面改动时
      //vue的数据是异步的不会立即渲染，需要使用vue的this.$nextTick(()=>{})
      //这个方法类似于一个高级定时器，等vue渲染完成后才执行
      this.$nextTick(() => {
        this.$refs.namefiled.focus();
      });
    },
    updateGender(gender) {
      editUserInfo({ gender }).then((res) => {
        //弹窗提示
        this.$toast.success("更新成功");
        //更新vue数据
        this.$store.commit("setUserInfo", {
          ...this.userInfo, ////先展开数据,查看数据是否纰漏
          gender: gender, //
        });
        this.showPopup = false;
      });
    },
    updateBirthday() {
      let birthday = moment(this.currentDate).format("YYYY-MM-DD");
      editUserInfo({ birthday }).then((res) => {
        //弹窗提示
        this.$toast.success("更新成功");
        //更新vue数据
        this.$store.commit("setUserInfo", {
          ...this.userInfo, ////先展开数据,查看数据是否纰漏
          birthday: birthday, //更新日期
        });
        //(3)关闭弹窗
        this.showDatePick = false;
      });
    },
    afterRead(file) {
      //设置需要裁切的图片交给cropper裁切，这里需要使用base64编码的图片
      this.img = file.content;
      //弹出裁切框
      this.showCropper = true;
    },
    //裁切确认按钮，裁切之后，上传图片
    submitPhoto() {
      //获取截图Blob这种格式
      this.$refs.cropper.getCropBlob(blob => {
        const fd = new FormData();
        //URL.createObjectURL(file.file)=>blob类型参数
        fd.append("photo", blob);//接收值可以用file.file
        editUserAvatar(fd).then((res) => {
          //弹窗提示
          this.$toast.success("更新成功");
          //更新vuex数据
          this.$store.commit("setUserInfo", {
            ...this.userInfo,
            photo: res.data.data.photo,
          });
          //关闭裁切窗口
          this.showCropper = false;
          
        });
      });
    },
  },
  components: {
    VueCropper,
  },
};
</script>

<style lang="less">
.edit-container {
  .van-nav-bar__content {
    background-image: linear-gradient(to right bottom, red, yellow);
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  // 头像部分
  .van-image {
    width: 100%;
    padding: 20px 0;

    .van-image__img {
      width: 120px;
      height: 120px;
      margin: 0 auto;
    }
  }

  //文件上传
  .van-uploader {
    opacity: 0;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    width: 120px;
    height: 120px;
    .van-uploader__upload {
      width: 120px;
      height: 120px;
    }
  }
  //修改性别
  .van-popup--bottom {
    .van-nav-bar__content {
      background-image: linear-gradient(to right bottom, yellow, #000);
    }
  }
  // 裁切弹框样式
  .cropper-box {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    .confirm-btn {
      position: absolute;
      left: 0;
      bottom: 0;
    }
    .cancel-btn {
      position: absolute;
      right: 0;
      bottom: 0;
    }
  }
}
</style>